var pageSize = 10;
var pageTotal = 0;
var pageIndex = 1;
var classifyId = 0;
var userId = 0;
var dataList = '';
$(function() {
    userId = $.GetRequest().userId;
    if (userId) {
        window.localStorage.setItem("userId", userId)
    } else {
        userId = window.localStorage.getItem("userId")
    }
    FastClick.attach(document.body);
    $(".fac-type-rightList").infinite();
    swiperImgList();
    factoryList()
});
//轮播图
function swiperImgList() {
    $.ajax({
        url: "/api/banner-ad/banner/index",
        type: "get",
        data: {
            type: 5,
            cityId:0,
        },
        success: function(res) {
            console.log(res);
            var img = res.data;
            var swiperImgList = [];
            $.each(img, function(index, val) {
                if (val.url_type == 0) {//不跳转
                    swiperImgList += '<div class="swiper-slide"><a class="goDetails" href="javascript:void(0);"><img src="' + val.img + '"/></a></div>'
                } else {//跳转
                    swiperImgList += '<div class="swiper-slide"><a class="goDetails" onclick="urlHref(' + val.url_value + ')"><img src="' + val.img + '"/></a></div>'
                }
            });
            $('#fac-swiper').html(swiperImgList);
            var swiper = new Swiper('.swiper-container',{
                autoplay: 3000,
                loop: true,
                pagination: '.swiper-pagination',
                autoplayDisableOnInteraction: false,
            })
        },
        error: function(res) {
            $.toast(res.msg, 'text')
        }
    })
}
function factoryList() {
    $.ajax({
        url: "/api/press/category/list",
        type: "get",
        data: {},
        success: function(res) {
            var type = res.data;
            var typeList = [];
            $.each(type, function(index, val) {
                if (index == 0) {
                    classifyId = val.id;
                    getList(classifyId);
                    typeList += "<li class='fac-active' data-id='" + val.id + "'><a>" + val.name + "</a></li>"
                } else {
                    typeList += "<li  data-id='" + val.id + "'><a>" + val.name + "</a></li>"
                }
            });
            $('#factory-classify').html(typeList);
            $(".fac-btn-fl li").bind("click", function() {
                $('#pageLoading').show();
                $('#factory-rightli').children().remove();
                $(this).addClass("fac-active").siblings().removeClass('fac-active');
                dataList = '';
                loading = false;
                classifyId = $(this).attr("data-id");
                $('#pageNomore').addClass('displaynone');
                pageIndex = 1;
                getList(classifyId)
            })
        },
    })
}
function getList(categoryId) {
    $.ajax({
        url: "/api/press/list",
        type: "get",
        data: {
            categoryId: categoryId,
            pageIndex: pageIndex,
            pageSize: pageSize
        },
        success: function(res) {
            let list = res.data.list;
            let wordList = "";
            if (dataList.length == 0 && list.length == 0) {
                $('#factory-rightli').html('<div style="font-size:0.25rem;text-align:center;">暂无数据</div>');
                $('#pageLoading').hide()
            } else {
                if (list.length > 0) {
                    $.each(list, function(index, val) {
                        wordList += "<li>" + '<a onclick="urlHref(' + val.id + ')">' + "<div class=\"img\"><img src=\"" + val.head_img + "\"></div><div class=\"txt\"><ul class=\"fac-fr-ul\"><li><span class=\"txt1\">【公司】</span><span class=\"txt2\">" + val.company_name + "</span></li><li><span class=\"txt1\">【主营】</span><span class=\"txt2\">" + val.main_business + "</span></li><li><span class=\"txt1\">【地址】</span><span class=\"txt2\">" + val.address + "</span></li><li><span class=\"txt3\">联系电话：" + val.mobile + "</span></li></ul></div></a></li>"
                    });
                    dataList = wordList
                } else {
                    $.toast("没有更多数据了...", "text");
                    $('#pageLoading').hide();
                    loading = true;
                    return false
                }
                $('#pageLoading').hide();
                $('#factory-rightli').append(dataList)
            }
            loading = false
        },
    })
}
function urlHref(item) {
    var para = {};
    para.title = "印厂详情";
    para.type = "facDetails";
    para.url = '/template/facDetails.html?id=' + item;
    if (this.userId > 0) {
        para.url = '/template/facDetails.html?id=' + item + "&userId=" + this.userId
    }
    if (/(Android)/i.test(navigator.userAgent)) {
        jsAndroid.appPush(JSON.stringify(para))
    } else if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
        window.webkit.messageHandlers.appPush.postMessage(para)
    } else {
        window.location.href = para.url;
        alert("PC浏览器")
    }
}
var loading = false;
$(".fac-type-rightList").infinite(1).on("infinite", function() {
    if (loading)
        return;
    $('#pageNomore').addClass('displaynone');
    $('#pageLoading').show();
    setTimeout(function() {
        pageIndex++;
        getList(classifyId);
        $('#pageLoading').hide();
        $('#pageNomore').addClass('displaynone')
    }, 1500);
    loading = true
});
